/**
 * author:wos
 * time:2024-1-4
 * title:部门树查询列表
 */

async function list(ctx, next) {
    const sql = `select * from sys_dept where status = '0'`
    let rs = await global.query(sql)
    let data = formatMenu(rs,0)
    global.response(ctx, {
        "status":200,
        "code": 200,
        "data": data,
        "msg":'success'
    })
}


/**
 * 格式化部门
 * @param menu
 * @param parentId
 * @returns {[]}
 */
function formatMenu(menu, parentId){
    let result = []
    for(let i in menu){
        if(menu[i].parentId == parentId){
            let item = {
                "id": menu[i].deptId,
                "label": menu[i].deptName
            }
            let children = formatMenu(menu, menu[i].deptId)
            if(children.length > 0){
                item.children = children
            }
            result.push(item)
        }
    }
    //排序
    result.sort((a, b) => {
        return Number(a.orderNum) - Number(b.orderNum);
    })
    return result
}



module.exports = list